package cn.com.czpb.client.module.personal.acticity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.config.CODE;
import cn.com.czpb.client.model.event.ChangeVideoStartEvent;

import cn.com.czpb.client.module.MainActivity;
import cn.com.czpb.client.module.personal.WodeFragment;
import cn.com.czpb.client.module.personal.dialog.WifiDialog;
import cn.com.czpb.client.services.ClearCacheService;
import cn.com.czpb.client.utils.AAnim;
import cn.com.czpb.client.utils.GlideCatchUtil;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.TUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;


public class SystemSetActivity extends MToolBarActivity {
    @BindView(R.id.tv_cache_size)
    TextView tv_cache_size;
    private boolean isflag;

    @BindView(R.id.big_textsize)
    TextView big_textsize;
    @BindView(R.id.normal_textsize)
    TextView normal_textsize;
    @BindView(R.id.little_textsize)
    TextView little_textsize;
    @BindView(R.id.tv_wifi_type)
    TextView tv_wifi_type;
    @BindView(R.id.tv_iv_type)
    TextView tv_iv_type;
    @BindView(R.id.togglebtn)
    ToggleButton togglebtn;
    @BindView(R.id.togglebtn_net)
    ToggleButton togglebtn_net;

    @BindView(R.id.unlogin)
    Button unlogin;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;

    boolean ISSELECT;
    private AlertDialog.Builder mDeleteDialog;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 111:
                    deleteSuccess();
                    break;
            }
        }
    };
    @Override
    protected int setLayoutId() {
        return R.layout.activity_system_set;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        if(spu.getUser() != null){
            unlogin.setVisibility(View.VISIBLE);
        }
        tv_cache_size.setText(GlideCatchUtil.getInstance().getCacheSize());
        tagglebtn();
        int testsize = spu.getTextSize();
        initTextsize();
        switch (testsize) {
            case CODE.textSize_small:
                little_textsize.setTextColor(getResources().getColor(R.color.color_df3031));
                break;
            case CODE.textSize_normal:
                normal_textsize.setTextColor(getResources().getColor(R.color.color_df3031));
                break;
            case CODE.textSize_big:
                big_textsize.setTextColor(getResources().getColor(R.color.color_df3031));
                break;
        }

        if (spu.getWifi() != null) {
            if (spu.getWifi().equals("1")) {
                tv_wifi_type.setText("每次提醒");
            } else {
                tv_wifi_type.setText("提醒一次");
            }
        }
        if (spu.getShowImage() != null) {
            if (spu.getShowImage().equals("0")) {

                togglebtn_net.setChecked(false);
                ISSELECT = true;
            } else if (spu.getShowImage().equals("1")) {
                togglebtn_net.setChecked(true);
            }
        }

    }


    private void tagglebtn() {
        //加载图片
        togglebtn_net.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent = new Intent();
                if (isChecked == false) {
                    togglebtn_net.setChecked(false);
                    spu.setShowImage("0");

                    if (ISSELECT) {
                        TUtils.toast("移动网络加载图片，请注意您的流量");
                    }
                    intent.setAction(MainActivity.SHOW);
                    activity.sendBroadcast(intent);
                } else {
                    LogUtils.e("不加载图片");
                    togglebtn_net.setChecked(true);
                    spu.setShowImage("1");
                    TUtils.toast("移动网络不加载图片");
                    intent.setAction(MainActivity.UNSHOW);
                    activity.sendBroadcast(intent);
                }
            }

        });
        //推送
        togglebtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == false) {
                    //关闭推送
                    togglebtn.setChecked(false);
                    JPushInterface.stopPush(activity);
//
                } else {
                    togglebtn.setChecked(true);
                    JPushInterface.resumePush(activity);
                    TUtils.toast("关闭推送");
                }
            }
        });
    }

    @OnClick({
            R.id.cache_set_activity,
            R.id.textsize_set_activity,
            R.id.fwifi_set_activity,
            R.id.fwifivideo_set_activity,
            R.id.tuisong_set_activity,
    })
    public void ClickNoactivity(View view) {
        switch (view.getId()) {
            case R.id.cache_set_activity:
                deleteCache();
                break;
            case R.id.textsize_set_activity:
                break;

            case R.id.fwifi_set_activity:
//                WifiDialog dialog = new WifiDialog(activity, R.style.location_style, 0);
//                dialog.show();
                break;
            case R.id.fwifivideo_set_activity:
                WifiDialog dialog_video = new WifiDialog(activity, R.style.location_style, 1);
                dialog_video.show();
                break;
            case R.id.tuisong_set_activity:

                break;
        }
    }

    @de.greenrobot.event.Subscribe
    public void onEventMainThread(ChangeVideoStartEvent event) {
        if (spu.getWifi() != null) {
            if (spu.getWifi().equals("1")) {
                tv_wifi_type.setText("每次提醒");
            } else {
                tv_wifi_type.setText("提醒一次");
            }
        }
        if (spu.getShowImage() != null) {
            if (spu.getShowImage().equals("0")) {
                togglebtn_net.setChecked(false);
            } else if (spu.getShowImage().equals("1")) {
                togglebtn_net.setChecked(true);
            } else {

            }
        }
    }

    @OnClick({R.id.big_textsize, R.id.normal_textsize, R.id.little_textsize})
    public void ChangeTextSize(View view) {
        initTextsize();
        switch (view.getId()) {
            case R.id.big_textsize:
                spu.setTextSize(CODE.textSize_big);
                big_textsize.setTextColor(getResources().getColor(R.color.color_df3031));
                break;
            case R.id.normal_textsize:
                spu.setTextSize(CODE.textSize_normal);
                normal_textsize.setTextColor(getResources().getColor(R.color.color_df3031));
                break;
            case R.id.little_textsize:
                spu.setTextSize(CODE.textSize_small);
                little_textsize.setTextColor(getResources().getColor(R.color.color_df3031));
                break;

        }
    }

    public void initTextsize() {
        big_textsize.setTextColor(getResources().getColor(R.color.color_999999));
        normal_textsize.setTextColor(getResources().getColor(R.color.color_999999));
        little_textsize.setTextColor(getResources().getColor(R.color.color_999999));
    }


    private void deleteCache() {
        mDeleteDialog = new AlertDialog.Builder(activity);
        mDeleteDialog.setTitle("确定清除缓存信息")
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                }).setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                activity.startService(new Intent(activity, ClearCacheService.class));
                handler.sendEmptyMessageDelayed(111, 2000);
            }
        }).show();
    }

    private void deleteSuccess() {
        AlertDialog.Builder dilaog = new AlertDialog.Builder(activity);
        dilaog.setMessage("删除成功");
        dilaog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dilaog.show();
        tv_cache_size.setText(GlideCatchUtil.getInstance().getCacheSize());
        LogUtils.e(GlideCatchUtil.getInstance().getCacheSize() + "0000000");

    }

    @OnClick({R.id.user_set_activity,


            R.id.about_set_activity,
            R.id.mianze_set_activity,

            R.id.fankui_set_activity,
    })
    public void click(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.user_set_activity:
                isflag = true;
                if (spu.getUser() != null) {
                    intent.setClass(activity, AccountSetActivity.class);
                } else {
                    intent.setClass(activity, LoginActivity.class);
                }
                break;

            case R.id.fankui_set_activity:
                isflag = true;
                intent.setClass(activity, UserSuggestActivity.class);
                break;
            case R.id.about_set_activity:
                isflag = true;
                intent.setClass(activity, AboutActivity.class);
                break;
            case R.id.mianze_set_activity:
                isflag = true;
                intent.setClass(activity, MianZeActivity.class);
                break;
        }
        if (isflag) {
            activity.startActivity(intent);
            AAnim.ActivityStartAnimation(activity);
        }
    }

    @OnClick(R.id.unlogin)
    public void UnLogin(View view) {
        if (ShareSDK.getPlatformList() != null) {
            for (Platform pl : ShareSDK.getPlatformList()) {
                if (pl.isAuthValid()) {
                    pl.SSOSetting(true);
                    pl.removeAccount(true);
                }
            }
        }
//        TUtils.toast(spu.getUser().getWechat());
        spu.setUser(null);
//        LogUtils.e(spu.getUser().toString());
        //EventUtils.getEventUrl();
        Intent intent = new Intent();
        if (view != null) {
            intent.setAction(WodeFragment.ACTION_QUIT);

        } else {
            intent.setAction(WodeFragment.ACTION_QUIT_LOGIN);
        }
        activity.sendBroadcast(intent);
        activity.finish();

    }

}
